package com.imooc.controller;

import com.imooc.entity.Evaluation;
import com.imooc.entity.Member;
import com.imooc.exception.BusinessException;
import com.imooc.service.EvaluationService;
import com.imooc.service.MemberService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
public class MemberController {

    @Resource
    private MemberService memberService;

    @Resource
    private EvaluationService evaluationService;

    @GetMapping("/register")
    public ModelAndView showRegister() {
        return new ModelAndView("/register");
    }

    @PostMapping("/register")
    @ResponseBody
    public Map<String, String> register(String username, String password, String nickname, String verifyCode, HttpSession httpSession) {
        Map<String, String> result = new HashMap<>();
        String sessionVerifyCode = (String) httpSession.getAttribute("verifyCode");
        if (verifyCode == null ||
                !verifyCode.equalsIgnoreCase(sessionVerifyCode)) {
            result.put("code", "VC01");
            result.put("msg", "验证码错误");
        } else {
            try {
                memberService.createMember(username, password, nickname);
                result.put("code", "0");
                result.put("msg", "success");
            } catch (BusinessException e) {
                e.printStackTrace();
                result.put("code", e.getCode());
                result.put("msg", e.getMsg());
            }
        }
        return result;
    }

    @GetMapping("/login")
    public ModelAndView showLogin() {
        return new ModelAndView("/login");
    }

    @PostMapping("/checkLogin")
    @ResponseBody
    public Map<String, String> checkLogin(String username, String password, String verifyCode, HttpSession httpSession)
    {
        Map<String, String> result = new HashMap<>();
        String sessionVerifyCode = (String) httpSession.getAttribute("verifyCode");
        if (verifyCode == null ||
                !verifyCode.equalsIgnoreCase(sessionVerifyCode)) {
            result.put("code", "VC01");
            result.put("msg", "验证码错误");
        } else {
            try {
                Member member = memberService.checkLogin(username, password);
                httpSession.setAttribute("loginMember", member);
                result.put("code", "0");
                result.put("msg", "success");
            } catch (BusinessException e) {
                e.printStackTrace();
                result.put("code", e.getCode());
                result.put("msg", e.getMsg());
            }
        }
        return result;
    }

    @PostMapping("/update_read_state")
    @ResponseBody
    public Map<String, String> updateMemberReadState(Long memberId, Long bookId, Integer readState)
    {
        Map<String, String> result = new HashMap<>();
        try {
            memberService.updateMemberReadState(memberId, bookId, readState);
            result.put("code", "0");
            result.put("msg", "success");
        } catch (BusinessException e) {
            e.printStackTrace();
            result.put("code", e.getCode());
            result.put("msg", e.getMsg());
        }
        return result;
    }

    @PostMapping("/evaluate")
    @ResponseBody
    public Map<String, String> evaluation(Long memberId, Long bookId, Integer score, String content)
    {
        Map<String, String> result = new HashMap<>();
        try {
            evaluationService.evaluate(memberId, bookId, score, content);
            result.put("code", "0");
            result.put("msg", "success");
        } catch (BusinessException ex) {
            ex.printStackTrace();
            result.put("code", ex.getCode());
            result.put("msg", ex.getMsg());
        }
        return result;
    }
    @PostMapping("/enjoy")
    @ResponseBody
    public Map<String, Object> enjoy(Long evaluationId)
    {
        Map<String, Object> result = new HashMap<>();
        try {
            Evaluation evaluation = evaluationService.enjoy(evaluationId);
            result.put("code", "0");
            result.put("msg", "success");
            result.put("evaluation", evaluation);
        } catch (BusinessException ex) {
            ex.printStackTrace();
            result.put("code", ex.getCode());
            result.put("msg", ex.getMsg());
        }
        return result;
    }
}
